//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>

#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE 5000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool SplitColumn(char* buffer, TListE<String>* columns, const char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    String* cell = new String(part);
    cell->Trim();
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main(int argc, char** argv) {

  String* filenameListIn;
  String* locationIn;
  String* locationOut;

  if (argc < 4) {
    cout << "Error in the input arguments. The execution must be: " << endl;
    cout << "./WriteDBFromListALocation <Input list file> <Location Database In> <Location Database Out>" << endl;
    return 0;
  }
  else {
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "  Input List File : " << argv[1] << endl;
    cout << "      Location In : " << argv[2] << endl;
    cout << "     Location Out : " << argv[3] << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    filenameListIn = new String(argv[1]);
    locationIn     = new String(argv[2]);
    locationOut    = new String(argv[3]);
  }

  fstream          fileList;
  char             buffer[LENGTHLINE + 1];
  TListE <String>* listFiles = new TListE <String> ();

  fileList.open(filenameListIn->Out(), fstream::in);
  while (!fileList.eof()) {
    fileList.getline(buffer, LENGTHLINE);
    String* line = new String(buffer);
    line->RTrim();
    listFiles->Add(line);
  }
  fileList.close();

  if (listFiles->Length()) {

    listFiles->SetInitial();
    while (listFiles->SetNext()) {

      char completePathIn[locationIn->Length() + listFiles->GetCurrent()->Length() + 30];
      sprintf(completePathIn, "%s%s\0", locationIn->Out(), listFiles->GetCurrent()->Out());

      char completePathOut[locationOut->Length() + listFiles->GetCurrent()->Length() + 30];
      sprintf(completePathOut, "%s%s\0", locationOut->Out(), listFiles->GetCurrent()->Out());

      char command[strlen(completePathIn) + strlen(completePathOut) + 100];
      sprintf(command, "cp %s %s\0", completePathIn, completePathOut);

      system(command);
      cout << command << endl;

    }

  }

  if (listFiles)      delete listFiles;

  if (filenameListIn) delete filenameListIn;
  if (locationIn)     delete locationIn;
  if (locationOut)    delete locationOut;

  return 1;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
